package com.example.wendao.view.ui.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.core.app.ShareCompat;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.wendao.App;
import com.example.wendao.R;
import com.example.wendao.common.ImgUtils;
import com.example.wendao.databinding.FragmentTalkDetailBinding;
import com.example.wendao.model.entity.Forum;
import com.example.wendao.model.entity.ForumPics;
import com.example.wendao.model.entity.NewsPics;
import com.example.wendao.model.entity.User;
import com.example.wendao.view.viewholder.TalkViewHolder;
import com.example.wendao.viewmodel.ForumViewModel;
import com.example.wendao.viewmodel.UserViewModel;


public class TalkDetailFragment extends Fragment {

    private FragmentTalkDetailBinding binding;

    private ForumViewModel forumViewModel;

    private UserViewModel userViewModel;

    private Bundle bundle;

    public TalkDetailFragment(){}

    User author;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_talk_detail, container, false);
        forumViewModel = new ViewModelProvider(App.getInstance()).get(ForumViewModel.class);
        userViewModel = new ViewModelProvider(App.getInstance()).get(UserViewModel.class);
        int forumid = 0;
        bundle = getArguments();
        if (bundle != null){
            forumid = bundle.getInt("forumid");
        }

        Forum forum = forumViewModel.getForumById(forumid);

        if (forum != null){
            int authorId = forum.getForumAuthorId();
            author = userViewModel.getUserById(authorId);

            String Title = forum.getForumTitle();
            binding.plantDetailNewsTitle.setText(Title);
            String Time = forum.getForumTime();
            binding.plantNewTime.setText(Time);
            binding.plantDescription.setText(forum.getForumContent());

            ForumPics forumPics = forumViewModel.findForumPicsCoverByFatherId(forum.getForumId());
            if (forumPics != null){
                byte[] bytes = forumPics.getForumPic();
                binding.newsDetailImage.setImageBitmap(ImgUtils.barrayToBitmap(bytes));
            }
        }

        binding.btnAgree.setOnClickListener(v -> {
            if (author != null){
                binding.btnAgree.setEnabled(false);
                forum.setForumAgree(forum.getForumAgree() + 1);
                forumViewModel.updateForum(forum);
                Toast.makeText(getContext(), "点赞成功", Toast.LENGTH_SHORT).show();
            }
        });

        binding.btnFollow.setOnClickListener(v -> {
            if (author != null){
                binding.btnFollow.setEnabled(false);
                userViewModel.follow(author);
                Toast.makeText(getContext(), "关注成功", Toast.LENGTH_SHORT).show();
            }
        });

        binding.toolbar.setNavigationOnClickListener(v -> {
            Navigation.findNavController(v).navigateUp();
        });
        binding.toolbar.setOnMenuItemClickListener(item -> {
            if (item.getItemId() == R.id.action_share) {
                createShareIntent();
                return true;
            }
            return false;
        });

        return binding.getRoot();
    }


    private void createShareIntent(){
        String shareText = "分享文本";
        Intent shareIntent = ShareCompat.IntentBuilder.from(getActivity())
                .setText(shareText)
                .setType("text/plain")
                .createChooserIntent()
                .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        startActivity(shareIntent);
    }

}
